import json
import ffmpeg


def get_audio_info(in_file):
    try:
        probe = ffmpeg.probe(in_file)
        audio_stream = next(
            (stream for stream in probe["streams"] if stream["codec_type"] == "audio"),
            None,
        )
        print("音频信息: audio_stream=", json.dumps(audio_stream, indent=4))

    except ffmpeg.Error as e:
        print(f"错误: {e.stderr.decode()}")
    except Exception as e:
        print(f"发生未知错误: {e}")


if __name__ == "__main__":
    file_path = "../../video/I_has.mp3"
    get_audio_info(file_path)

audio_stream = {
    "index": 0,
    "codec_name": "mp3",
    "codec_long_name": "MP3 (MPEG audio layer 3)",
    "codec_type": "audio",
    "codec_tag_string": "[0][0][0][0]",
    "codec_tag": "0x0000",
    "sample_fmt": "fltp",
    "sample_rate": "44100",
    "channels": 2,
    "channel_layout": "stereo",
    "bits_per_sample": 0,
    "r_frame_rate": "0/0",
    "avg_frame_rate": "0/0",
    "time_base": "1/14112000",
    "start_pts": 0,
    "start_time": "0.000000",
    "duration_ts": 918281952,
    "duration": "65.071000",
    "bit_rate": "128000",
    "disposition": {
        "default": 0,
        "dub": 0,
        "original": 0,
        "comment": 0,
        "lyrics": 0,
        "karaoke": 0,
        "forced": 0,
        "hearing_impaired": 0,
        "visual_impaired": 0,
        "clean_effects": 0,
        "attached_pic": 0,
        "timed_thumbnails": 0
    }
}